//
//  QSLocalNotificationManager.swift
//  TestSwift
//
//  Created by Song on 2018/7/24.
//  Copyright © 2018年 Song. All rights reserved.
//

import UIKit

class QSLocalNotificationManager: NSObject {
    /// 获取本地推送授权
    class func qs_requestAuthority() {
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (accepted, error) in
                if !accepted {
                    //                print("用户不允许消息通知。")
                }
            }
        } else {
            let settings = UIUserNotificationSettings.init(types: [UIUserNotificationType.badge, .alert, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(settings)
        }
    }
    
    /// 发送本地推送
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - body: 正文
    ///   - badge: 桌面图标的上角标
    ///   - userInfo: 自定义推送的内容
    ///   - timeInterval: 定时发送，默认立即发送
    ///   - identifier: 该条推送的唯一标识
    class func qs_sendNotification(title: String, body: String, badge: NSNumber, userInfo: [AnyHashable : Any], timeInterval: TimeInterval = 0, identifier: String? = nil) {
        if #available(iOS 10.0, *) {
            // 设置推送内容
            let content = UNMutableNotificationContent()
            content.title = title
            content.body = body
            content.badge = badge
            content.userInfo = userInfo
            
            // 设置通知触发器
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false)
            
            // 设置请求标识符
            let requestIdentifier = identifier
            //设置一个通知请求
            let request = UNNotificationRequest(identifier: requestIdentifier ?? "",
                                                content: content, trigger: trigger)
            
            // 将通知请求添加到发送中心
            UNUserNotificationCenter.current().add(request) { error in
                if error == nil {
                    // 成功
                }
            }
        } else {
            // 创建UILocalNotification来进行本地消息通知
            let localNotification = UILocalNotification()
            // 推送时间
            localNotification.fireDate = Date.init(timeIntervalSinceNow: timeInterval)
            // 时区
            localNotification.timeZone = NSTimeZone.default
            // 推送内容
            localNotification.alertBody = body
            // 声音
            localNotification.soundName = UILocalNotificationDefaultSoundName
            // 额外信息
            var uInfo = userInfo
            uInfo["identifier"] = identifier
            localNotification.userInfo = uInfo
            UIApplication.shared.scheduleLocalNotification(localNotification)
        }
    }
    
    /// 取消本地通知
    ///
    /// - Parameter identifier: 唯一标识   iOS10以后有这个属性，之前版本需要在userInfo中自己有添加identifier的字典键值对
    class func qs_cancelNotification(identifier: String) {
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])
        } else {
            var existingNotification: UILocalNotification?
            
            let allNotifications = UIApplication.shared.scheduledLocalNotifications
            for notification in allNotifications! {
                let info = notification.userInfo as! [String: String]
                let notId = info["identifier"]
                if notId != nil && notId == identifier {
                    existingNotification = notification
                    break
                }
                
                existingNotification = nil
            }
            
            if existingNotification != nil {
                // 取消消息推送
                UIApplication.shared.cancelLocalNotification(existingNotification!)
            }
        }
    }
}
